package com.song.common.util;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class LocalDateTimeUtil {
    public static final String DEFAULT = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_DATE = "yyyy-MM-dd";
    public static final String DATE_TIME = "EEE MMM dd HH:mm:ss zzz yyyy";

    /**
     * Date转换为LocalDateTime
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date){
        return LocalDateTime.from(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
    }

    /**
     * LocalDateTime转换为Date
     * @param localDateTime
     * @return
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime){
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * LocalDateTime转换为时间戳
     * @param localDateTime
     * @return
     */
    public static Long localDateTimeToTimeStamp(LocalDateTime localDateTime){
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 时间戳转换为LocalDateTime
     * @param timestamp
     * @return
     */
    public static LocalDateTime timeStampToLocalDateTime(Long timestamp){
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
    }

    /**
     * LocalDateTime格式化
     * @param localDateTime
     * @param pattern
     * @return
     */
    public static String localDateTimeToString(LocalDateTime localDateTime, String pattern){
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 日期字符串转换为LocalDateTime
     * @param resource
     * @param pattern
     * @return
     */
    public static LocalDateTime stringToLocalDateTime(String resource, String pattern){
        return LocalDateTime.parse(resource, DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 日期字符串转换为LocalDate
     * @param resource
     * @param pattern
     * @return
     */
    public static LocalDate stringToLocalDate(String resource, String pattern){
        return LocalDate.parse(resource, DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 日期字符串转换为LocalTime
     * @param resource
     * @param pattern
     * @return
     */
    public static LocalTime stringToLocalTime(String resource, String pattern){
        return LocalTime.parse(resource, DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 转换时间格式
     * @param source
     * @return
     */
    public static String format(String source, String sourcePattern, String newPattern){
        LocalDateTime localDateTime = LocalDateTime.parse(source, DateTimeFormatter.ofPattern(sourcePattern));
        return localDateTime.format(DateTimeFormatter.ofPattern(newPattern));
    }

    /**
     * 获取一天的起始时间
     * @param localDateTime
     * @return
     */
    public static LocalDateTime getTodayStart(LocalDateTime localDateTime){
        return LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MIN);
    }

    /**
     * 获取一天的结束时间
     * @param localDateTime
     * @return
     */
    public static LocalDateTime getTodayEnd(LocalDateTime localDateTime){
        return LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MAX);
    }

    /**
     * 获取当月的起始时间
     * @param localDateTime
     * @return
     */
    public static LocalDateTime getMonthStart(LocalDateTime localDateTime){
        LocalDate date = LocalDate.of(localDateTime.getYear(), localDateTime.getMonth(), 1);
        return LocalDateTime.of(date, LocalTime.MIN);
    }

    /**
     * 获取当月的结束时间
     * @param localDateTime
     * @return
     */
    public static LocalDateTime getMonthEnd(LocalDateTime localDateTime){
        Month month = localDateTime.getMonth();
        LocalDate date = LocalDate.of(localDateTime.getYear(), month, month.length(localDateTime.toLocalDate().isLeapYear()));
        return LocalDateTime.of(date, LocalTime.MAX);
    }

    /**
     * 获取当年的起始时间
     * @param localDateTime
     * @return
     */
    public static LocalDateTime getYearStart(LocalDateTime localDateTime){
        LocalDate date = LocalDate.of(localDateTime.getYear(), Month.JANUARY, 1);
        return LocalDateTime.of(date, LocalTime.MIN);
    }

    /**
     * 获取当年的结束时间
     * @param localDateTime
     * @return
     */
    public static LocalDateTime getYearEnd(LocalDateTime localDateTime){
        LocalDate date = LocalDate.of(localDateTime.getYear(), Month.DECEMBER, Month.DECEMBER.length(localDateTime.toLocalDate().isLeapYear()));
        return LocalDateTime.of(date, LocalTime.MAX);
    }

    /**
     * 获取num月前的时间
     * @param localDateTime
     * @param num
     * @return
     */
    public static LocalDateTime numMonthBefore(LocalDateTime localDateTime, long num){
        return localDateTime.minusMonths(num);
    }

    /**
     * 获取月剩余天数
     * @param localDateTime
     * @return
     */
    public static int remainingDaysOfMonth(LocalDateTime localDateTime){
        int dayOfMonth = localDateTime.getDayOfMonth();
        int length = localDateTime.getMonth().length(localDateTime.toLocalDate().isLeapYear());
        return length - dayOfMonth + 1;
    }

    /**
     * 获取年剩余天数
     * @param localDateTime
     * @return
     */
    public static int remainingDaysOfYear(LocalDateTime localDateTime){
        int dayOfYear = localDateTime.getDayOfYear();
        return localDateTime.toLocalDate().isLeapYear()? 365 - dayOfYear + 1 : 366 - dayOfYear + 1;
    }
}
